from _keys import YOUR_USERID,YOUR_APIKEY

CHARACTER = "Dew Black"
WantDivs = 'Security', 'Internal Security', 'Command', 'Surveillance', 'Intelligence'
WantLVL = 4,
WantTypes = 'BasicAgent',
HubSystem = 'Jita'
MaxDistance = 50
MinSecurity = 0.5
MaxSecurity = 100

from api import eveapi,cache
import cPickle

db=cPickle.load(file('eve.db','rb'))

ctx=eveapi.EVEAPIConnection(cacheHandler=cache.CacheHandler()).auth(userID=YOUR_USERID, apiKey=YOUR_APIKEY)

skills=ctx.eve.SkillTree()
social=skills.skillGroups.IndexedBy('groupName').Get('Social').skills.IndexedBy('typeName')
sidConnections=social.Get('Connections').typeID
sidDiplomacy=social.Get('Diplomacy').typeID
sidNegotiation=social.Get('Negotiation').typeID

print 'Characters:'
for n,char in enumerate(ctx.account.Characters().characters):
    if char.name==CHARACTER:
        print '    %d) %-20s [#%s] *SELECTED*'%(n,char.name,char.characterID)
        SelectedID=char.characterID
    else:
        print '    %d) %-20s [#%s]'%(n,char.name,char.characterID)
print '--'


chctx=ctx.char
chctx.parameters['characterID']=SelectedID

stand=chctx.Standings().standingsFrom
stFaction=dict((db.Faction[x.fromID],x.standing) for x in stand.factions)
stCorp=dict((db.Corp[x.fromID],x.standing) for x in stand.NPCCorporations)
stAgent=dict((db.Agent[x.fromID],x.standing) for x in stand.agents)

skills=chctx.CharacterSheet().skills
if skills.Get(sidConnections,None):
    skConnections=skills.Get(sidConnections).level
else:
    skConnections=0

if skills.Get(sidDiplomacy,None):
    skDiplomacy=skills.Get(sidDiplomacy).level
else:
    skDiplomacy=0

if skills.Get(sidNegotiation,None):
    skNegotiation=skills.Get(sidNegotiation).level
else:
    skNegotiation=0


print 'Skills:'
print '    Connections: ',skConnections,'/ 5'
print '    Diplomacy: ',skDiplomacy,'/ 5'
print '    Negotiation: ',skNegotiation,'/ 5'
print '--'

WantDivs=set(x for x in db.Division.values() if x.name in WantDivs)
WantTypes=set(x for x in db.AgentType.values() if x.name in WantTypes)
WantLVL=set(WantLVL)
HubSystem=db.SolarName[HubSystem]
HubSystem.calcPMap()

class Standing(object):
    kind=None
    stand=-100
    mod=None
    eff=-100
    def add(self,kind,stand):
        if stand>self.stand:
            self.stand=stand
            self.kind=kind
            if stand>0:
                self.mod=(10-stand)*(0.04*skConnections)
            else:
                self.mod=(10-stand)*(0.04*skDiplomacy)
            self.eff=stand+self.mod

    def __cmp__(self,v):
        return cmp(self.eff,v.eff)

    def __repr__(self):
        return '%.2f(+%.2f)[%s]'%(self.stand,self.mod,self.kind)

class Result(object):
    def __init__(self,agent):
        self.agent=agent
        self.needSt=(agent.lvl-1)*2+agent.quality/20.0

        self.standing=Standing()
        if agent in stAgent: self.standing.add('A',stAgent[agent])
        if agent.corp in stCorp: self.standing.add('C',stCorp[agent.corp])
        if agent.corp.faction in stFaction: self.standing.add('F',stFaction[agent.corp.faction])

        self.effQuality=agent.quality+self.standing.eff+5*skNegotiation+(1-agent.station.system.security)*40

        if agent.station:
            self.jumps=HubSystem.DMap[agent.station.system]
        else: self.jumps=1000000

        sys={agent.station.system:True}
        n=0
        while True:
            nsys={}
            n+=1
            for x in sys.keys():
                for y in x.links:
                    sys[y]=True
                    if y.security<=0.5:
                        self.nLowSec=n
                        self.cnSys='%s (J:%d)'%(y.name,n)
                        return
            if n>4: break
        self.nLowSec=100
        self.cnSys='Too Far'

    def test(self):
        if self.agent.division not in WantDivs: return False
        if self.agent.lvl not in WantLVL: return False
        if self.agent.kind not in WantTypes: return False
        if self.agent.station.system.security<=MinSecurity: return False
        if self.agent.station.system.security>MaxSecurity: return False
        if self.standing.eff<self.needSt: return False
        if self.jumps>MaxDistance: return False
        return True


    def __str__(self):
        agent=self.agent
        res='%s L:%d Q:%d[%d] SS:%.2f J:%d ST:%r LS:%s'%(agent.name,agent.lvl,self.effQuality,self.agent.quality,agent.station.system.security,self.jumps,self.standing,self.cnSys)
        res+='\n %s / %s / %s'%(agent.corp.faction.name,agent.corp.name,agent.division.name)
        res+='\n '+agent.station.name+'\n'
        return res

    def __cmp__(self,v):
        return cmp(self.effQuality,v.effQuality)

Results=[]
for agent in db.Agent.values():
    if agent.quality is None: continue
    if agent.station is None: continue
    t=Result(agent)
    if t.test(): Results.append(t)

print '--\n'.join(str(x) for x in sorted(Results))